package com.ruoyi.web.controller.house;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.cost.domain.HouseDoorCost;
import com.ruoyi.cost.service.IHouseDoorCostService;
import com.ruoyi.costQuery.domain.HouseCostT;
import com.ruoyi.costQuery.service.IHouseCostQueryTService;
import com.ruoyi.data.DataUtils;
import com.ruoyi.data.MD5;
import com.ruoyi.door.domain.HouseDoorT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 户资金修改Controller
 * 
 * @author ruoyi
 * @date 2023-04-26
 */
@RestController
@RequestMapping("/house/cost")
public class HouseDoorCostController extends BaseController
{
    @Autowired
    private IHouseDoorCostService houseDoorCostService;

    @Autowired
    private IHouseCostQueryTService houseCostQueryTService;

    /**
     * 查询户管理列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseDoorT houseDoorT) throws Exception {
        startPage();
        if(houseDoorT.getUserName()!=null){
            houseDoorT.setUserName(MD5.encrypt(houseDoorT.getUserName()));
        }
        List<HouseDoorT> list = houseDoorCostService.selectHouseCostTList(houseDoorT);
        if(list.size()!=0) {
            for (HouseDoorT houseDoorVO : list) {
                houseDoorVO.setUserName(MD5.decrypt(houseDoorVO.getUserName()));
            }
            if (getDataTableSumJudge(list)) {
                HouseDoorT houseDoorVO = houseDoorCostService.selectHouseCostSum(houseDoorT);
                houseDoorVO.setAreaName("合计");
                list.add(houseDoorVO);
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取户管理详细信息
     */
    /**
     * 获取缴费信息详细信息
     */
    @GetMapping(value = "/{costId}")
    public AjaxResult getInfo(@PathVariable("costId") Long costId)
    {
        return AjaxResult.success(houseCostQueryTService.selectHouseCostTByCostId(costId));
    }
    /**
     * 新增户资金修改
     */
    @Log(title = "缴费信息修改", businessType = BusinessType.INSERT)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody HouseDoorT houseDoorT) throws Exception {
        HouseDoorT doorTempVO = new HouseDoorT();
        HouseDoorCost houseDoorCostT = new HouseDoorCost();
        doorTempVO = houseDoorCostService.selectHouseDoorCostTByCostId(houseDoorT);
        if(DataUtils.subMoney(doorTempVO.getCostMoney(),houseDoorT.getCostMoney()).compareTo(doorTempVO.getBalance())>=0d){
            throw new Exception("余额不足不允许修改！");
        }
        houseDoorCostT.setCostMoneyPre(doorTempVO.getCostMoney());
        houseDoorCostT.setCostDatePre(doorTempVO.getCostDate());
        houseDoorCostT.setBalancePre(doorTempVO.getBalance());
        houseDoorCostT.setCreater(houseDoorT.getCreater());
        houseDoorCostT.setBillNo(houseDoorT.getBillNo());
        houseDoorCostT.setBillNoPre(doorTempVO.getBillNo());
        houseDoorCostT.setRemark(houseDoorT.getRemark());
        houseDoorCostT.setHouseId(houseDoorT.getHouseId());
        houseDoorCostService.updateHouseCostT(houseDoorT);
        HouseDoorT houseDoorVO=houseDoorCostService.selectCostMoneySum(houseDoorT);
        if(houseDoorVO==null){
            houseDoorVO = new HouseDoorT();
            houseDoorVO.setCostMoney(0d);
        }
        HouseDoorT houseDoorVO3=houseDoorCostService.selectRepairPayMoneySum(houseDoorT);
        if(houseDoorVO3==null){
            houseDoorVO3 = new HouseDoorT();
            houseDoorVO3.setPayMoney(0d);
        }
        HouseDoorT houseDoorVO4=houseDoorCostService.selectRateMoneySum(houseDoorT);
        if(houseDoorVO4==null){
            houseDoorVO4 = new HouseDoorT();
            houseDoorVO4.setRateMoney(0d);
        }
        houseDoorT.setBalanceSum(DataUtils.subMoney(DataUtils.sumMoney(houseDoorVO.getCostMoney(),houseDoorVO4.getRateMoney()),houseDoorVO3.getPayMoney()));
        houseDoorT.setBalance(DataUtils.subMoney(houseDoorVO.getCostMoney(),houseDoorVO3.getPayMoney()));
        houseDoorT.setCostMoneySum(String.valueOf(houseDoorVO.getCostMoney()));
        houseDoorT.setCostDate(houseDoorT.getCostDate());
        houseDoorT.setEndDate(houseDoorVO.getCostDate());
        houseDoorT.setBillNo(houseDoorT.getBillNo());
        houseDoorT.setBillNoPre(doorTempVO.getBillNo());
        houseDoorT.setCostMoneyPre(doorTempVO.getCostMoney());
        houseDoorT.setCostDatePre(doorTempVO.getCostDate());
        houseDoorT.setBalancePre(doorTempVO.getBalance());
        houseDoorT.setCreater(getUsername());
        houseDoorT.setRemark(houseDoorT.getRemark());
        return toAjax(houseDoorCostService.updateHouseDoorT(houseDoorT));
    }
}
